package co.edu.usbcali.prototipobeta.utilities;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
//package co.com.eficacia.integracionhv.equivalencias.utilities;

/*
 * Created on Feb 21, 2005
 *
 */
import java.io.File;
import java.io.FileInputStream;
import java.security.Security;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import com.sun.mail.util.LineInputStream;

public class GoogleTest {

	//private static final String emailMsgTxt = "Test Message Contents";
	//private static final String emailSubjectTxt = "A test from gmail";
	//private static final String emailFromAddress = "jbejarano@vortexbird.com";
	//private static final String[] sendTo = {"johan_bejarano@hotmail.com"};

	   /* public static void main(String args[]) throws Exception {
	
	    	Class clase = Class.forName("com.sun.mail.util.LineInputStream");
	    	
	        //Test
	        new GoogleTest().sendSSLMessage(
	                   new String[]{"jbejarano@vortexbird.com"},
	                   "Error Interface de... ",
	                   "Se informa que .....",
	                   "jose.edward10@gmail.com",
	                   "jose.edward10@gmail.com",
	                   "EDWARD910420");
	
	        System.out.println("Sucessfully Sent mail to All Users");
	    }*/
	 
	public void sendMail(
			String recipients[],
			String subject,
			String message,
			String from,
			final String user,
			final String password,
			Properties props) throws Exception{

		boolean debug = true;
		Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

		Session session = Session.getDefaultInstance(props,
				new javax.mail.Authenticator() {

			@Override
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(user, password);
			}
		});

		//        Session session = Session.getDefaultInstance(props);

		session.setDebug(debug);

		Message msg = new MimeMessage(session);
		InternetAddress addressFrom = new InternetAddress(from);
		msg.setFrom(addressFrom);

		InternetAddress[] addressTo = new InternetAddress[recipients.length];
		for (int i = 0; i < recipients.length; i++) {
			addressTo[i] = new InternetAddress(recipients[i]);
		}
		msg.setRecipients(Message.RecipientType.TO, addressTo);

		// Setting the Subject and Content Type
		msg.setSubject(subject);
		//msg.setContent(message, "text/plain");
		msg.setContent(message,
				"text/html; charset=ISO-8859-1");
		Transport.send(msg);
	}
	
	public void sendMail2(
			String recipients[],
			String subject,
			String message,
			String from,
			final String user,
			final String password,
			File attachment,
			Properties props) throws Exception{

		boolean debug = true;
		Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

		Session session = Session.getDefaultInstance(props,
				new javax.mail.Authenticator() {

			@Override
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(user, password);
			}
		});

		//        Session session = Session.getDefaultInstance(props);

		session.setDebug(debug);

		Message msg = new MimeMessage(session);
		InternetAddress addressFrom = new InternetAddress(from);
		msg.setFrom(addressFrom);

		InternetAddress[] addressTo = new InternetAddress[recipients.length];
		for (int i = 0; i < recipients.length; i++) {
			addressTo[i] = new InternetAddress(recipients[i]);
		}
		msg.setRecipients(Message.RecipientType.TO, addressTo);

		// Setting the Subject and Content Type
		msg.setSubject(subject);
		//msg.setContent(message, "text/plain");
		/*msg.setContent(message,
				"text/html; charset=ISO-8859-1");
		Transport.send(msg);*/
		
		
		 // Create the message part 
        BodyPart messageBodyPart = new MimeBodyPart();

        // Fill the message
        messageBodyPart.setText(message);

        // Create a multipar message
        Multipart multipart = new MimeMultipart();

        // Set text message part
        multipart.addBodyPart(messageBodyPart);

        // Part two is attachment
        if (attachment.isFile() && attachment.exists()){
	        messageBodyPart = new MimeBodyPart();
	        String filename = attachment.getName();
	        DataSource source = new FileDataSource(attachment);
	        messageBodyPart.setDataHandler(new DataHandler(source));
	        messageBodyPart.setFileName(filename);
	        multipart.addBodyPart(messageBodyPart);
        }

        // Send the complete message parts
        msg.setContent(multipart);

        // Send message
        Transport.send(msg);
	}

	

	public void sendSSLMessage(String recipients[], String subject,
			String message, String from, String user,
			String password
			) throws Exception {

		
		
		
		final String SMTP_HOST_NAME = "smtp.gmail.com";
		final String SMTP_PORT = "465";
		final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";

		Properties props = new Properties(); 

		File f = new File(getClass().getResource("mail.properties").getPath());

		if (f.exists()){
			props.load(new FileInputStream(f));
		}else{
			props.put("mail.smtp.host", SMTP_HOST_NAME);
			props.put("mail.smtp.auth", "true");
			props.put("mail.debug", "true");
			props.put("mail.smtp.port", SMTP_PORT);
			props.put("mail.smtp.socketFactory.port", SMTP_PORT);
			props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
			props.put("mail.smtp.socketFactory.fallback", "false");
		}

		sendMail(recipients, subject, message, from, user, password, props);

	}
	
	public void sendSSLMessage2(String recipients[], String subject,
			String message, String from, String user,
			String password, File archivo
			) throws Exception {

		
		
		
		final String SMTP_HOST_NAME = "smtp.gmail.com";
		final String SMTP_PORT = "465";
		final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";

		Properties props = new Properties(); 

		File f = new File(getClass().getResource("mail.properties").getPath());

		if (f.exists()){
			props.load(new FileInputStream(f));
		}else{
			props.put("mail.smtp.host", SMTP_HOST_NAME);
			props.put("mail.smtp.auth", "true");
			props.put("mail.debug", "true");
			props.put("mail.smtp.port", SMTP_PORT);
			props.put("mail.smtp.socketFactory.port", SMTP_PORT);
			props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
			props.put("mail.smtp.socketFactory.fallback", "false");
		}

		sendMail2(recipients, subject, message, from, user, password, archivo, props);

	}
	
	
}
